/**
 * 
 */
package com.taikang.t.web.action;

import java.util.List;

import org.apache.struts2.config.ParentPackage;
import org.springframework.beans.factory.annotation.Required;

import com.taikang.common.util.PointUtils;
import com.taikang.common.util.RequestUtils;
import com.taikang.common.util.StringUtils;
import com.taikang.common.util.struts.SimpleActionSupport;
import com.taikang.t.helper.IKHelpeer;
import com.taikang.t.helper.StanfordParserHelper;
import com.taikang.t.model.BasicWord;
import com.taikang.t.model.FashionWord;
import com.taikang.t.model.FullWord;
import com.taikang.t.model.NormalWord;
import com.taikang.t.model.ProfessionalWord;
import com.taikang.t.service.impl.*;


/**
 * @author fee
 *
 */
@ParentPackage("struts-default")
public class TalkAction extends SimpleActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = -4931206890187887163L;
	private FullWordManager fullWordManager;
	private FashionWordManager fashionWordManager;
	private ProfessionalWordManager professionalWordManager;
	private NormalWordManager normalWordManager;
	private BasicWordManager basicWordManager;
	private RandomWordManager randomWordManager;
	
	@Required
	public void setFullWordManager(FullWordManager service)
	{
		this.fullWordManager = service;
	}
	@Required
	public void setFashionWordManager(FashionWordManager service)
	{
		this.fashionWordManager = service;
	}
	@Required
	public void setProfessionalWordManager(ProfessionalWordManager service)
	{
		this.professionalWordManager = service;
	}
	@Required
	public void setNormalWordManager(NormalWordManager service)
	{
		this.normalWordManager = service;
	}
	@Required
	public void setBasicWordManager(BasicWordManager service)
	{
		this.basicWordManager = service;
	}
	@Required
	public void setRandomWordManager(RandomWordManager service)
	{
		this.randomWordManager = service;
	}
	
	public String talk() {
		// TODO Auto-generated method stub
		//接收到客户的对话
		String say = RequestUtils.getParameter("say");
		String pureSay = PointUtils.pointCut(say);
		String answer = findFullWord(pureSay);
		
		if(StringUtils.isEmpty(answer))
		{
			//先分词
			String sentence = IKHelpeer.distinguish(say);
			//解析语法
			StanfordParserHelper sfph = new StanfordParserHelper(sentence); 
			
			
			String keyWord = sfph.getKeys();
			

				answer = findFashionWord(keyWord);
				if(StringUtils.isEmpty(answer))
				{
					answer = findProfessionalWord(keyWord);
					if(StringUtils.isEmpty(answer))
					{
						answer = findNormalWord(keyWord);
						if(StringUtils.isEmpty(answer))
						{
							answer = findBasicWord(keyWord);
							if(StringUtils.isEmpty(answer))
							{
								answer = findRandomWord();
								if(StringUtils.isEmpty(answer))
								{
									answer = "小T已经无话可说了。。";
								}
							}
						}
					}
				}
				
		}
		try {
			jsonOkResponse(answer);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	
	@SuppressWarnings("unchecked")
	public String findFullWord(String say)
	{
		List answers = fullWordManager.findAnswers(say);
		Integer count = answers.size();
		if(count>0)
		{
			Integer i = (int)(Math.random()*count);
			FullWord fw = (FullWord) answers.get(i);
			return fw.getAnswer();
		}else
		{
			return null;
		}
	}

	
	@SuppressWarnings("unchecked")
	public String findFashionWord(String say)
	{
		List answers = fashionWordManager.findAnswers(say);
		Integer count = answers.size();
		if(count>0)
		{
			Integer i = (int)(Math.random()*(count));
			FashionWord fw = (FashionWord) answers.get(i);
			return fw.getAnswer();
		}else
		{
			return null;
		}
	}
	
	@SuppressWarnings("unchecked")
	public String findProfessionalWord(String say)
	{
		List answers = professionalWordManager.findAnswers(say);
		Integer count = answers.size();
		if(count>0)
		{
			Integer i = (int)(Math.random()*(count));
			ProfessionalWord fw = (ProfessionalWord) answers.get(i);
			return fw.getAnswer();
		}else
		{
			return null;
		}
	}
	
	@SuppressWarnings("unchecked")
	public String findNormalWord(String say)
	{
		List answers = normalWordManager.findAnswers(say);
		Integer count = answers.size();
		if(count>0)
		{
			Integer i = (int)(Math.random()*(count));
			NormalWord fw = (NormalWord) answers.get(i);
			return fw.getAnswer();
		}else
		{
			return null;
		}
	}
	
	@SuppressWarnings("unchecked")
	public String findBasicWord(String say)
	{
		List answers = basicWordManager.findAnswers(say);
		Integer count = answers.size();
		if(count>0)
		{
			Integer i = (int)(Math.random()*(count));
			BasicWord fw = (BasicWord) answers.get(i);
			return fw.getAnswer();
		}else
		{
			return null;
		}
	}
	
	public String findRandomWord()
	{
		String fw =  randomWordManager.findAnswer();
		if(fw==null)
		{
			return null;
		}
		return fw;
	}
	
}
